package com.mano.mall.gateway.handler.impl;

import com.mano.mall.common.base.BaseResponse;
import com.mano.mall.common.constants.Constants;
import com.mano.mall.gateway.handler.GatewayHandler;
import com.netflix.zuul.context.RequestContext;

/**
 * 相同的Handler业务逻辑实现重构
 */
public class BaseHandler {
	public GatewayHandler gatewayHandler;

	public GatewayHandler getNextHandler() {
		return gatewayHandler;
	}

	public void setNextHandler(GatewayHandler gatewayHandler) {
		if (gatewayHandler != null)
			this.gatewayHandler = gatewayHandler;
	}

	public void resultError(Integer code, RequestContext ctx, String errorMsg) {
		ctx.setResponseStatusCode(code);
		// 网关响应为false 不会转发服务
		ctx.setSendZuulResponse(false);
		ctx.setResponseBody(errorMsg);
		ctx.getResponse().setContentType("text/html;charset=UTF-8");

	}

	// 接口直接返回true 或者false
	public Boolean isSuccess(BaseResponse<?> baseResp) {
		if (baseResp == null) {
			return false;
		}
		if (!baseResp.getCode().equals(Constants.HTTP_RES_CODE_200)) {
			return false;
		}
		return true;
	}

	public Boolean isSuccessData(BaseResponse<?> baseResponse){
		if(isSuccess(baseResponse)){
			Object responseData = baseResponse.getData();
			return responseData == null? false:true;
		}
		return false;
	}

}
